package com.atzon.controller;

import com.atzon.service.ArtService;
import com.atzon.service.MeetService;
import com.atzon.service.UserService;
import com.atzon.service.impl.ArtServiceImpl;
import com.atzon.service.impl.MeetServiceImpl;
import com.atzon.service.impl.UserSrviceImpl;
import com.atzon.utils.PageUtil;
import com.atzon.utils.StrUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;
import java.util.Set;

@WebServlet("/view/*")
public class UIServlet extends BaseServlet {
    private UserService userService = new UserSrviceImpl();
    private ArtService artService = new ArtServiceImpl();
    private MeetService meetService = new MeetServiceImpl();

    public void home(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher(req.getContextPath() + "/html/home.jsp").forward(req, resp);
    }

    public void department(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            List<Map<String, Object>> depts = meetService.findDept();
            req.setAttribute("depts", depts);
            List<Map<String, Object>> deptUsers = meetService.findDeptUser();
            req.setAttribute("deptUsers", deptUsers);
            req.getRequestDispatcher(req.getContextPath() + "/html/department.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void article(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String currentPage = req.getParameter("page");
            String search = req.getParameter("search");
            if(null != search && !StrUtil.isContainChinese(search)){
                search = URLDecoder.decode(search,"UTF-8");
                byte[] bytes = search.getBytes("ISO-8859-1");
                search = new String(bytes,"UTF-8");
            }
            int page = 1;
            if (null != currentPage) {
                page = Integer.parseInt(currentPage);
            }
            String userId = req.getSession().getAttribute("userId").toString();
            List<Map<String, Object>> list = artService.findArtList(Integer.parseInt(userId), page, search);
            req.setAttribute("artList", list);
            long totalPage = PageUtil.getTotalPage(artService.findTotalArtCount(Integer.parseInt(userId)));
            long startPage = PageUtil.getStartPage(page) + 1;
            long endPage = PageUtil.getEndPage(page, totalPage);
            req.setAttribute("totalPage", totalPage);
            req.setAttribute("currentPage", page);
            req.setAttribute("startPage", startPage);
            req.setAttribute("endPage", endPage);
            req.setAttribute("search", search);
            req.getRequestDispatcher(req.getContextPath() + "/html/article.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void artAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher(req.getContextPath() + "/html/article_add.jsp").forward(req, resp);
    }

    public void artCollect(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String currentPage = req.getParameter("page");
            String search = req.getParameter("search");
            if(null != search && !StrUtil.isContainChinese(search)){
                search = URLDecoder.decode(search,"UTF-8");
                byte[] bytes = search.getBytes("ISO-8859-1");
                search = new String(bytes,"UTF-8");
            }
            int page = 1;
            if (null != currentPage) {
                page = Integer.parseInt(currentPage);
            }
            String userId = req.getSession().getAttribute("userId").toString();
            List<Map<String, Object>> list = artService.findFavList(Integer.parseInt(userId), page, search);
            req.setAttribute("favList", list);
            long totalPage = PageUtil.getTotalPage(artService.findTotalFavCount(Integer.parseInt(userId)));
            long startPage = PageUtil.getStartPage(page) + 1;
            long endPage = PageUtil.getEndPage(page, totalPage);
            req.setAttribute("totalPage", totalPage);
            req.setAttribute("currentPage", page);
            req.setAttribute("startPage", startPage);
            req.setAttribute("endPage", endPage);
            req.setAttribute("search", search);
            req.getRequestDispatcher(req.getContextPath() + "/html/article_collect.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void artDetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String artId = req.getParameter("artId");
            String detail = req.getParameter("det");
            req.setAttribute("detail", detail);
            artService.addCount(Integer.parseInt(artId));
            Map<String, Object> art = artService.findArtById(Integer.parseInt(artId));
            req.setAttribute("art", art);
            List<Map<String, Object>> favUsers = artService.findFavUser(Integer.parseInt(artId));
            req.setAttribute("favUsers", favUsers);
            req.getRequestDispatcher(req.getContextPath() + "/html/article_detail.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void meeting(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String currentPage = req.getParameter("page");
            String search = req.getParameter("search");
            if(null != search && !StrUtil.isContainChinese(search)){
                search = URLDecoder.decode(search,"UTF-8");
                byte[] bytes = search.getBytes("ISO-8859-1");
                search = new String(bytes,"UTF-8");
            }
            String dept = req.getParameter("dep");
            if(null != dept && !StrUtil.isContainChinese(dept)){
                dept = URLDecoder.decode(dept,"UTF-8");
                byte[] bytes = dept.getBytes("ISO-8859-1");
                dept = new String(bytes,"UTF-8");
            }
            int page = 1;
            if (null != currentPage) {
                page = Integer.parseInt(currentPage);
            }
            List<Map<String, Object>> list = meetService.findMeet(page, search, dept);
            req.setAttribute("meetList", list);
            List<Map<String, Object>> depts = meetService.findDept();
            req.setAttribute("depts", depts);
            long totalPage = PageUtil.getTotalPage(meetService.findTotalMeetCount(search,dept));
            long startPage = PageUtil.getStartPage(page) + 1;
            long endPage = PageUtil.getEndPage(page, totalPage);
            req.setAttribute("totalPage", totalPage);
            req.setAttribute("currentPage", page);
            req.setAttribute("startPage", startPage);
            req.setAttribute("endPage", endPage);
            req.setAttribute("search", search);
            req.setAttribute("dep", dept);
            req.getRequestDispatcher(req.getContextPath() + "/html/meeting.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void meetAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            List<Map<String, Object>> depts = meetService.findDept();
            req.setAttribute("depts", depts);
            req.getRequestDispatcher(req.getContextPath() + "/html/meeting_add.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void meetDetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String meetId = req.getParameter("meetId");
            Map<String, Object> meet = meetService.findMeetById(Integer.parseInt(meetId));
            req.setAttribute("meet", meet);
            req.getRequestDispatcher(req.getContextPath() + "/html/meeting_detail.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void user(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String currentPage = req.getParameter("page");
            String search = req.getParameter("search");
            if(null != search && !StrUtil.isContainChinese(search)){
                search = URLDecoder.decode(search,"UTF-8");
                byte[] bytes = search.getBytes("ISO-8859-1");
                search = new String(bytes,"UTF-8");
            }
            int page = 1;
            if (null != currentPage) {
                page = Integer.parseInt(currentPage);
            }
            String userId = req.getSession().getAttribute("userId").toString();
            List<Map<String, Object>> list = userService.findList(Integer.parseInt(userId), page, search);
            req.setAttribute("userList", list);
            long totalPage = PageUtil.getTotalPage(userService.findTotalUserCount(search));
            long startPage = PageUtil.getStartPage(page) + 1;
            long endPage = PageUtil.getEndPage(page, totalPage);
            req.setAttribute("totalPage", totalPage);
            req.setAttribute("currentPage", page);
            req.setAttribute("startPage", startPage);
            req.setAttribute("endPage", endPage);
            req.setAttribute("search", search);
            //再去后台查询用户的基本信息
//            Map<String, Object> map = userService.findUserBaseById(Integer.parseInt(userId));
//            req.setAttribute("userBaseInfo", map);
            req.getRequestDispatcher(req.getContextPath() + "/html/user.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void userDetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String uId = req.getParameter("uId");
            Map<String, Object> detailInfo = userService.getDetail(Integer.parseInt(uId));
            req.setAttribute("detailInfo", detailInfo);
            req.getRequestDispatcher("/html/user_detail.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void userLook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            Map<String, Object> userBaseInfo = (Map<String, Object>)req.getSession().getAttribute("userBaseInfo");
            Integer userId = (Integer)userBaseInfo.get("id");
            Map<String, Object> detailInfo = userService.getDetail(userId);
            req.setAttribute("detailInfo", detailInfo);
            List<Map<String, Object>> depts = meetService.findDept();
            req.setAttribute("depts", depts);
            req.getRequestDispatcher(req.getContextPath() + "/html/user_look.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void myUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String userId = req.getSession().getAttribute("userId").toString();
            String currentPage = req.getParameter("page");
            int page = 1;
            if (null != currentPage) {
                page = Integer.parseInt(currentPage);
            }
            List<Map<String, Object>> focusUser = userService.getFocus(Integer.parseInt(userId),page);
            long totalPage = PageUtil.getTotalPage(userService.findTotalFocusCount(Integer.parseInt(userId)));
            long startPage = PageUtil.getStartPage(page) + 1;
            long endPage = PageUtil.getEndPage(page, totalPage);
            req.setAttribute("totalPage", totalPage);
            req.setAttribute("currentPage", page);
            req.setAttribute("startPage", startPage);
            req.setAttribute("endPage", endPage);
            req.setAttribute("focusUser", focusUser);
            req.getRequestDispatcher(req.getContextPath() + "/html/my_user.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
